
$(function () {
    // 获取统计数据
    function dataInfo() {
        $.get('/admin/data/info', function (res) {
            $('.totalCount em').html(res.totalArticle)
            $('.totalCount_day em').html(res.dayArticle)
            $('.commentCount em').html(res.totalComment)
            $('.commentCount_day em').html(res.dayComment)
        })
    }
    // 日新增文章趋势图
    function dayArticle() {
        $.get('/admin/data/article', function (res) {
            // console.log(res.date)
            let date = []
            let num = []
            for (let key in res.date) {
                date[date.length] = res.date[key].date
                num[num.length] = res.date[key].count
            }
            const myChart = echarts.init(document.getElementById('day_article_show'));
            const option = {
                xAxis: {
                    name: '日期',
                    type: 'category',
                    data: date,
                    boundaryGap: false,
                },
                yAxis: {
                    name: '数量',
                    type: 'value',
                },
                title: {
                    left: 'center',
                    text: '日新增文章趋势',
                },
                series: [{
                    data: num,
                    type: 'line',
                    smooth: true,
                    itemStyle: {
                        color: 'rgb(255, 70, 131)'
                    },
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: 'rgb(255, 70, 131)'
                        }, {
                            offset: 1,
                            color: 'rgb(255, 158, 68)'
                        }])
                    },
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                }],

            };
            myChart.setOption(option);
        })

    }


    // 获取分类饼状图
    function dataCategory() {
        $.get('/admin/data/category', function (res) {
            console.log(res.date)
            let data = res.date
            const myChart = echarts.init(document.getElementById('pie_show'));
            const option = {
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    left: 'left',
                },
                color: ['#5885e8', '#13cfd5', '#00ce68', '#ff9565', '#20ff19'],
                series: [
                    {
                        type: 'pie',
                        radius: ['35%', '50%'],
                        data: data.map(item => ({ value: item.articles, name: item.name })),
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            myChart.setOption(option);

        })
    }

    // 日文章访问量
    function dataVisit() {
        $.get('/admin/data/article', function (res) {
            console.log(res)
            let data = res.date
            let date = []
            let num = []
            for (let key in data) {
                date[date.length] = data[key].date
                num[num.length] = data[key].count
            }
            const myChart = echarts.init(document.getElementById('column_show'));
            const option = {
                xAxis: {
                    type: 'category',
                    data: date
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: num,
                    type: 'bar'
                }]
            };

            myChart.setOption(option);
        })
    }

    // 获取统计数据
    dataInfo()
    // 日新增文章趋势图
    dayArticle()
    // 获取分类饼状图
    dataCategory()
    // 日文章访问量
    dataVisit()
})